package com.zhangdi.dwperms.token;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zhangdi.dwcache.CacheContents;
import com.zhangdi.dwperms.commons.Contents;
import com.zhangdi.dwperms.enums.LoginType;

import java.util.Date;

public class MSToken implements AuthToken {
    private String principal;
    private long expiration;
    private long issuedAt;

    @JsonIgnore
    private String credentials;
    @JsonIgnore
    private LoginType loginType = LoginType.token;

    public MSToken() {
        Date now = new Date();
        this.expiration = now.getTime() + Contents.EXPIRE_TIME;
        this.issuedAt = now.getTime();
    }

    public long getExpiration() {
        return expiration;
    }

    public long getIssuedAt() {
        return issuedAt;
    }

    public MSToken setPrincipal(String principal) {
        this.principal = principal;
        return this;
    }

    public MSToken setCredentials(String credentials) {
        this.credentials = credentials;
        return this;
    }

    public MSToken setExpiration(long expiration) {
        this.expiration = expiration;
        return this;
    }

    public MSToken setIssuedAt(long issuedAt) {
        this.issuedAt = issuedAt;
        return this;
    }

    public LoginType getLoginType() {
        return loginType;
    }

    public void setLoginType(LoginType loginType) {
        this.loginType = loginType;
    }

    @Override
    public Date issuedAt() {
        return new Date(issuedAt);
    }

    @Override
    public Date expiration() {
        return new Date(expiration);
    }

    @Override
    public Object getPrincipal() {
        return principal;
    }

    @Override
    public Object getCredentials() {
        return credentials;
    }

    @Override
    public String toString() {
        return "MSToken{" +
                "principal='" + principal + '\'' +
                ", issuedAt=" + issuedAt +
                ", expiration=" + expiration +
                ", credentials='" + credentials + '\'' +
                '}';
    }

    @Override
    public String cacheKey() {
        return CacheContents.REDIS_AUTHC_USER_KEY + principal;
    }
}
